package com.selvn.encryptnote;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.net.Uri;
import android.text.TextUtils;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.selvn.db.*;

public class NotesContentProvider extends ContentProvider {
	public static final String NOTES_TYPE = "vnd.android.cursor.dir/note";
	public static final String NOTES_ITEM_TYPE = "vnd.android.cursor.item/note";
	
	public static final String AUTHORITY = "com.selvn.provider.notesprovider";
	
	public static final Uri NOTES_URI = Uri.parse( "content://" + AUTHORITY + "/notes");
	
	private DBOpenHelper dbOpenHelper = null;
	
	// 自定义匹配码, 只是容易看, 没实际意义
	public static final int NOTE = 1;
	public static final int NOTES = 2;
	
	private static final UriMatcher uriMatcher;
	static {
		uriMatcher = new UriMatcher( UriMatcher.NO_MATCH);
		uriMatcher.addURI(AUTHORITY, "note", NOTES);
		uriMatcher.addURI(AUTHORITY, "note/#", NOTE);
	}
	
	@Override
	public boolean onCreate()
	{
		dbOpenHelper = new DBOpenHelper(this.getContext());
		return true;
	}
	
	@Override
	public int update( Uri uri, ContentValues values, String selection, String[] selectionArgs )
	{
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		int count = 0;
		switch (uriMatcher.match(uri)) {
		case NOTES:
			count = db.update(Config.DBTABLE, values, selection, selectionArgs);
			break;
		case NOTE:
			long noteid = ContentUris.parseId(uri);
			String whereString = Config.DBID + "=" + noteid;
			whereString += !TextUtils.isEmpty(selection) ? " and ("+ selection +")" : "";
			count = db.update(Config.DBTABLE, values, whereString, selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		db.close();
		return count;
	}
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs)
	{
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		int count = 0;
		switch (uriMatcher.match(uri)) {
		case NOTES:
			count = db.delete(Config.DBTABLE, selection, selectionArgs);
			break;
		case NOTE:
			long noteid = ContentUris.parseId(uri);
			String whereString = "_id=" + noteid;
			// 附上其它条件
			whereString += !TextUtils.isEmpty(selection) ? " and (" + selection + ")" : "";
			count = db.delete(Config.DBTABLE, whereString, selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		db.close();
		return count;
	}
	@Override
	public Cursor query( Uri uri, String[] columns, String selection, String[] selectionArgs, String orderBy)
	{
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		
		switch (uriMatcher.match(uri)) {
		// 查询全部记录
		case NOTES:
			return db.query("notes", columns, selection, selectionArgs, null, null, orderBy);
		// 查询特定的数据
		case NOTE:
			long noteid = ContentUris.parseId(uri);
			String whereString = "_id=" + noteid;
			// 把传过来的其它条件加上
			whereString += !TextUtils.isEmpty(selection) ? " and (" + selection + ")" : "";
			return db.query("notes", columns, whereString, selectionArgs, null, null, orderBy);
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
	}
	@Override
	public Uri insert( Uri uri, ContentValues values )
	{
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		long id = 0;
		switch (uriMatcher.match(uri)) {
		case NOTES:
			id = db.insert(Config.DBTABLE, "title", values);
			return ContentUris.withAppendedId(uri, id);
		case NOTE:
			id = db.insert(Config.DBTABLE, "title", values);
			String path = uri.toString();
			return Uri.parse(path.substring(0, path.lastIndexOf("/")) + id );
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
	}
	
	@Override
	public String getType( Uri uri)
	{
		switch (uriMatcher.match(uri)) {
		case NOTES:
			return NOTES_TYPE;
		case NOTE:
			return NOTES_ITEM_TYPE;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
	}
}
